package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeDTO.EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        //  后期需要进行md5加密，然后再进行比对 FINISH
        password = DigestUtils.md5DigestAsHex(password.getBytes()); //不需要加盐
        System.out.println(password);
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus().equals(StatusConstant.DISABLE)) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    /**
     * 创建员工
     * @param employeeDTO
     */
    @Override
    public void save(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();

        //将Dto 转换成普通属性实用 sprig提供的工具
        BeanUtils.copyProperties(employeeDTO,employee);

        //补充属性
        //设置默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        //状态
        employee.setStatus(StatusConstant.ENABLE);

        //创建时间 更新时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        //通过 JWT 令牌命名的现成名将期取出 23/12/22 23:09分完成
//        Long currentId = BaseContext.getCurrentId();
//        employee.setCreateUser(currentId);
//        employee.setUpdateUser(currentId);

        employeeMapper.inset(employee);

    }


    /**
     * 员工信息分页
     * @param employeePageQueryDTO
     * @return
     */
    @Override
    public PageResult PageQuery(EmployeePageQueryDTO employeePageQueryDTO) {

        //实用 mybatis 提供的 分类工具 pagehlep 输入参数 页面 和  每页显示记录数
        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());

        //查询sql语句返回员工信息
        Page<Employee> employees =  employeeMapper.pageQuery(employeePageQueryDTO);
        //将数据库返回的数据 提取出来封装到 PageResult中返回到Con
        long total = employees.getTotal();
        List<Employee> result = employees.getResult();

        return new PageResult(total,result);
    }

    /**
     * 员工账号状态更新
     * @param status
     * @param id
     */
    @Override
    public void Update(Integer status, Long id) {
        //将参数封装成一个实体
//        Employee.EmployeeBuilder employee = Employee.builder();

        Employee employee = new Employee();
        employee.setStatus(status);
        employee.setId(id);

        employeeMapper.Update(employee);

    }

    /**
     * 根据id 查询员工信息
     * @param id
     * @return
     */
    @Override
    public Employee getById(Integer id) {
        Employee employee  = employeeMapper.getById(id);
        //增加安全性
        employee.setPassword("**********");
        return employee;
    }

    /**
     * 保存员工修改信息
     * @param employee
     */
    @Override
    public void Update(EmployeeDTO employeeDTO) {
//        Employee.EmployeeBuilder employee = Employee.builder();

        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);//copy一下

//        employee.setUpdateTime(LocalDateTime.now());  //修改时间
//        employee.setUpdateUser(BaseContext.getCurrentId()); //修改人
//        System.out.println("员工信息DTO：" + employeeDTO.toString());
//        System.out.println("员工信息2：" + employee.toString());

        employeeMapper.Update(employee);

    }
}
